const api = require("../../api/sop");
const { err, toast } = require("../../utils/wxapi");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    history: [],
    chunkedArray: [],
    page: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const history = wx.getStorageSync("history");
    this.setData({ history });
    const chunkedArray = this.splitArray(history, 20);
    this.setData({ chunkedArray });
    this.getlist();
  },
  async getlist() {
    try {
      const { chunkedArray, page, list } = this.data;
      const data = JSON.stringify({ workIdList: chunkedArray[page] });
      const res = await api.histort(data);
      console.log(res);
      if (res.respCode === 200) {
        const newlist = JSON.parse(res.source);
        const updatelist = page > 0 ? [...list, ...newlist] : newlist;
        this.setData({ list: updatelist });
      }
    } catch (error) {
      err(error);
    }
  },
  splitArray(array, chunkSize) {
    const chunks = [];
    for (let i = 0; i < array.length; i += chunkSize) {
      const chunk = array.slice(i, i + chunkSize);
      chunks.push(chunk);
    }
    return chunks;
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { chunkedArray, page } = this.data;
    if (chunkedArray[page + 1]) {
      this.setData({ page: page + 1 });
      this.getlist();
    } else {
      toast("暂无更多浏览记录");
    }
  },
});
